package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.Page;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper ;

    @Autowired
    private CategoryMapper categoryMapper ;

    //添加套餐
    @Override
    @Transactional
    public void addSetmeal(SetmealDto setmealDto) {

        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealMapper.addSetmeal(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getCreateUser());
            setmealDish.setSort(0);
            setmealMapper.addSetmealDish(setmealDish);
        }

    }

    //回应套餐列表
    @Override
    public Result page(Integer page, Integer pageSize, String name) {
        List<Category> allByType = categoryMapper.findAllByType(2);

        HashMap< Long , String > map = new HashMap<>();

        for (Category category : allByType) {
            map.put(category.getId() , category.getName());
        }

        PageHelper.startPage(page , pageSize);

        List<Setmeal> setmeals = setmealMapper.findAll(name);

        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(setmeals);

        List<Setmeal> list = setmealPageInfo.getList();

        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();

        for (Setmeal setmeal : list) {

            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties( setmeal , setmealDto );

            setmealDto.setCategoryName(map.get(setmeal.getCategoryId()));

            setmealDtos.add(setmealDto);

        }

        Page page1 = new Page();

        page1.setTotal(setmealPageInfo.getTotal());

        page1.setRecords(setmealDtos);

        return Result.success(page1) ;
    }

    //删除套餐
    @Override
    @Transactional
    public Result delete(Long[] ids) {
        if( ids.length == 0 ){
            throw new BusinessException("没有选中删除的数据");
        }
        for (Long id : ids) {
            Setmeal onSaleById = setmealMapper.findOnSaleById(id);
            if( onSaleById != null ){
                throw new BusinessException("删除中的数据有正在出售的");
            }
        }

        for (Long id : ids) {
            setmealMapper.deleteById(id);
        }

        for (Long id : ids) {
            setmealMapper.deleteSetmealDishById(id);
        }

        return Result.success(null);
    }

    @Override
    public Result findByCategoryId(String  categoryId) {
        List<Setmeal> setmeals =  setmealMapper.findByCategoryId(categoryId);
        return Result.success(setmeals);
    }
}
